package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class _996NewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.996.com/news/[0-9]+/[0-9]+.html",
		//http://www.teiron.com/news.php?Id=40
		"http://www.teiron.com/news.php[?]Id=[0-9]+"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		String url=urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();

		if(!isDetailPage(url)){
		}


		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		/*
		 * 标题
		 */
		title=doc.select("div.newsDetail div.title h2").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("div.head h3").text();
		}

		/*
		 * 内容
		 */
		doc.select(".related").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select("div.dec"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.text"));
		}

		/*
		 * 时间
		 */
		String text=doc.select("span.time").text();
		date=DateUtils.matchDate(text);
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.text p").text());
		}
		/*
		 * 链接
		 */
		news.setUrl(url);

		/*
		 * 来源
		 */
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		_996NewsAnalyse analyse=new _996NewsAnalyse();
		String url="http://www.teiron.com/news.php?Id=40";
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}


	
	public boolean isNeedUpdate(){
		return false;
	}

}
